//
//  GDNewsViewController.m
//  爱新奇－味道
//
//  Created by 热带雨林 on 17/2/17.
//  Copyright © 2017年 热带雨林. All rights reserved.
//

#import "GDNewsViewController.h"
#import "GDNewsChannelView.h"
#import "Masonry.h"
#import "GDNewsChannelModel.h"
#import "GDNewsDetailsListController.h"


@interface GDNewsViewController ()<UIPageViewControllerDelegate,UIPageViewControllerDataSource>
@property(nonatomic, weak)GDNewsChannelView *newsChannelView;

//存储channelView的数据
@property (nonatomic, strong)NSArray<GDNewsChannelModel *> *channelModelList;

//控制器需要使用的是strong进行修饰
@property(nonatomic, strong)UIPageViewController *pageViewController;
@property(nonatomic, weak)UIScrollView *scrollView;


@end

@implementation GDNewsViewController{
    
    
    //开始记录一下上下两个控制器
    GDNewsDetailsListController *_currentListC;
    GDNewsDetailsListController *_nextListC;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"新闻资讯";
    self.tabBarItem.title = @"新闻";
    self.view.backgroundColor = [UIColor whiteColor];
    
}

- (void)setupUI{
    //添加滚动的label
    GDNewsChannelView *newsChannelV = [[GDNewsChannelView alloc]init];
    self.newsChannelView = newsChannelV;
    [self.view addSubview:newsChannelV];
    
    [newsChannelV addTarget:self action:@selector(clickChannel:) forControlEvents:UIControlEventValueChanged];
    
    [newsChannelV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view).offset(64);
        make.height.mas_equalTo(30);
    }];
    //加载滚滚动ChannelView中的数据
    //在控制器中获得数据
    NSArray *channelArray = [GDNewsChannelModel channelModel];
    newsChannelV.ChannelModelList = channelArray;
    self.channelModelList = channelArray;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self setpageVC];
}
//控制器中只能通过添加分页控制器的方式，将其中一个控制器的添加到另一个控制器中
- (void)setpageVC{
    self.pageViewController = [[UIPageViewController alloc]initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
    self.pageViewController.view.backgroundColor = [UIColor grayColor];
    self.pageViewController.delegate = self;
    self.pageViewController.dataSource = self;
    //通过channelView的中的数据，进行判断是第几个界面
    GDNewsChannelModel *model = self.channelModelList[0];
    GDNewsDetailsListController *startingViewController = [[GDNewsDetailsListController alloc]initWithTid:model.tid withCurrentIndexPath:0];
    NSArray *viewControllers = @[startingViewController];
    [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
    self.pageViewController.dataSource = self;
    [self addChildViewController:self.pageViewController];
    [self.view addSubview:self.pageViewController.view];
    [self.pageViewController.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_newsChannelView.mas_bottom);
        make.left.bottom.right.equalTo(self.view);
    }];
    //告诉外界，子VC已经加到父VC中
    [self.pageViewController didMoveToParentViewController:self];
    self.scrollView = self.pageViewController.view.subviews[0];
}
//使用page的代理进行翻页
//向前滚动
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(GDNewsDetailsListController *)viewController{
    NSInteger currentIndexP = viewController.indexP;
    //判断是否为第一个
    if(currentIndexP == 0){
        return nil;
    }
    currentIndexP --;
    //根据获得的index 创建控制器
    GDNewsChannelModel *model = self.channelModelList[currentIndexP];
    GDNewsDetailsListController *detailsListV = [[GDNewsDetailsListController alloc]initWithTid:model.tid withCurrentIndexPath:currentIndexP];
    return detailsListV;
}
//向后滚动
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(GDNewsDetailsListController *)viewController{
    NSInteger currentIndexP = viewController.indexP;
    if(currentIndexP == self.channelModelList.count - 1){
    //如果是最后一个就将不能在继续滚动
        return nil;
    }
    currentIndexP ++;
    GDNewsChannelModel *model = self.channelModelList[currentIndexP];
    GDNewsDetailsListController *detailsListV = [[GDNewsDetailsListController alloc]initWithTid:model.tid withCurrentIndexPath:currentIndexP];
    return detailsListV;
}
- (void)clickChannel:(GDNewsChannelView *)channelView{
    NSInteger currentIndexP = channelView.currentIndexP;
    //进行判断
    if(currentIndexP == _currentListC.indexP){
        return;
    }
    GDNewsChannelModel *model = self.channelModelList[currentIndexP];
    GDNewsDetailsListController *detailsLisTV = [[GDNewsDetailsListController alloc]initWithTid:model.tid withCurrentIndexPath:currentIndexP];
    NSArray *viewControllers = @[detailsLisTV];
    NSInteger direction = UIPageViewControllerNavigationDirectionForward;
    //判断一下滚动的方向
    if(currentIndexP < _currentListC.indexP){
    //向后滚动
        direction = UIPageViewControllerNavigationDirectionReverse;
    }
    //设置page控制的内容
    [self.pageViewController setViewControllers:viewControllers direction:direction animated:NO completion:nil];
    [self.newsChannelView channelLabelWithScale:1 indexP:currentIndexP];
    [self.newsChannelView channelLabelWithScale:0 indexP:_currentListC.indexP];
    _currentListC = detailsLisTV;
}
//开始使用page的代理方法
//开始翻页
- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray<GDNewsDetailsListController *> *)pendingViewControllers{
    //记录一下，当前的控制器与下一个控制器
    _currentListC = pageViewController.viewControllers[0];
    _nextListC = pendingViewControllers[0];
    //开起监听机制
    [self.scrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
}
//结束翻页
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed{
    //移除监听者
    [self.scrollView removeObserver:self forKeyPath:@"contentOffset"];
}
//开启监听者模式
//开始监听contentOffset
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(UIScrollView *)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    CGFloat x = object.contentOffset.x;
    //获得屏幕一次滚动的宽度
    CGFloat scrollW = self.pageViewController.view.frame.size.width;
    x = x - scrollW;
    x = x / scrollW;
    x = ABS(x);
    CGFloat nextScale = x;
    CGFloat currentScale = 1 - x;
    //由index 改变label的变化率
    [self.newsChannelView channelLabelWithScale:currentScale indexP:_currentListC.indexP];
    [self.newsChannelView channelLabelWithScale:nextScale indexP:_nextListC.indexP];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
